package com.lz.sports.bean

import androidx.room.*
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import com.lz.sports.utils.DateUtils
import com.lz.sports.utils.UploadUtils
import java.io.File
import java.lang.reflect.Type

//成绩确认表
@Entity(tableName = "ExamSure")
class ExamSure() {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "examSureId")
    var id: Int = 0

    //考生ID
    @ColumnInfo(name = "examineeId")
    var examineeId: Int = 0

    //考试计划ID
    @ColumnInfo(name = "examPlanId")
    var examPlanId: Int = 0

    //检录考区ID
    @ColumnInfo(name = "examAreaId")
    var examAreaId: Int = 0

    //检录的考点ID
    @ColumnInfo(name = "examSiteId")
    var examSiteId: Int = 0

    //所属项目ID
    @ColumnInfo(name = "projectId")
    var projectId: Int = 0

    @ColumnInfo(name = "projectName")
    var projectName: String = ""

    //成绩确认状态
    @ColumnInfo(name = "sureStatus")
    var sureStatus: Int = -1

    //成绩确认时间
    @ColumnInfo(name = "sureTime")
    var sureTime: Long = 0L

    //考试类型 9:JXN8-身份证-指纹-人脸（人照并人证)10:JXN9-身份证-人脸（人照并人证）-指纹
    @ColumnInfo(name = "examType")
    var examType: Int = 0

    //验证类型(通过)0人脸1指纹2手动
    @ColumnInfo(name = "verificationType")
    var verificationType: Int = -1

    //现场照片
    @ColumnInfo(name = "sceneImage")
    var sceneImage: String = ""

    //身份证照片
    @ColumnInfo(name = "cardImage")
    var cardImage: String = ""

    //报名照片
    @ColumnInfo(name = "photoImage")
    var photoImage: String = ""

    //身份证匹对结果
    @ColumnInfo(name = "idCardPhotoMate")
    var idCardPhotoMate: Float = 0f

    //报名照匹对结果
    @ColumnInfo(name = "examPhotoMate")
    var examPhotoMate: Float = 0f

    //指纹匹对结果
    @ColumnInfo(name = "fingerMate")
    var fingerMate: Float = 0f

    //验证是否通过：0未验证1通过2验证不通过3.存疑
    @ColumnInfo(name = "validateStatus")
    var validateStatus: Int = 0

    //现场照是否上传成功、
    @ColumnInfo(name = "scenePhotoUploadStatus")
    var scenePhotoUploadStatus = -1

    //身份证照否上传成功、
    @ColumnInfo(name = "idCardPhotoUploadStatus")
    var idCardPhotoUploadStatus = -1

    //报考照是否上传成功
    @ColumnInfo(name = "examPhotoUploadStatus")
    var examPhotoUploadStatus = -1

    //数据是否已经上传
    @ColumnInfo(name = "uploadStatus")
    var uploadStatus = -1

    //电子签名图片
    @ColumnInfo(name = "signatureImage")
    var signatureImage: String = ""

    //电子签名照是否上传成功
    @ColumnInfo(name = "signaturePhotoUploadStatus")
    var signaturePhotoUploadStatus = -1

    //身份证识别地址
    fun idCardImgFile(): File = File(UploadUtils.examSurePath(), cardImage)

    //现场照地址
    fun sceneImgFile(): File = File(UploadUtils.examSurePath(), sceneImage)

    //报名照地址
    fun examImgFile(): File = File(UploadUtils.examSurePath(), photoImage)

    //电子签名照地址
    fun signatureImgFile(): File = File(UploadUtils.examSurePath(), signatureImage)

    @Ignore
    constructor(result: ExamResult, examType: Int, sureTime: Long): this() {
        this.examineeId = result.examineeId
        this.examPlanId = result.examPlanId
        this.examAreaId = result.examAreaId
        this.examSiteId = result.examSiteId
        this.projectId = result.projectId
        this.projectName = result.projectName
        this.examType = examType
        this.sureTime = sureTime
    }

    fun sureTimeStr(): String = DateUtils.format(sureTime)

    class ExamSureSerializer : JsonSerializer<ExamSure> {
        override fun serialize(src: ExamSure, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement {
            val json = JsonObject()
            json.addProperty("studentId", src.examineeId)
            json.addProperty("examAreaId", src.examAreaId)
            json.addProperty("examPlanId", src.examPlanId)
            json.addProperty("examSiteId", src.examSiteId)
            json.addProperty("examProjectId", src.projectId)

            json.addProperty("examType", src.examType)
            json.addProperty("cardImage", src.cardImage)
            json.addProperty("fingerprintPercentage", src.fingerMate)
            json.addProperty("personPhotoPercentage", src.idCardPhotoMate)
            json.addProperty("sceneImage", src.sceneImage)
            json.addProperty("signlmage", src.signatureImage)
            json.addProperty("validateStatus", src.validateStatus)
            json.addProperty("verificationPercentage", src.validateStatus)
            json.addProperty("verificationType", src.verificationType)
            json.addProperty("verificationTime", DateUtils.format2(src.sureTime))
            return json
        }
    }
}